#include "snake.h"
#include "ui_snake.h"
#include <QPainter>
#include<QTimer>
#include<QKeyEvent>
snake::snake(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::snake),run(false),speed(1000)
{
    ui->setupUi(this);
    this->setGeometry(QRect(600,300,290,310));//画出窗体
}

snake::~snake()
{
    delete ui;
}
void snake::paintEvent(QPaintEvent *event){
    QPainter painter(this);//画出游戏界面
    if(!run)
    initalsnake();
    painter.setPen(Qt::black);
    painter.setBrush(Qt::gray);
    painter.drawRect(15,15,260,260);//外围
    painter.setPen(Qt::black);
    painter.setBrush(Qt::black);
    painter.drawRect(20,20,250,250);//内围
    painter.setPen(Qt::white);
    for(int i=2;i<=27;i++){
        painter.drawLine(20,i*10,270,i*10);
        painter.drawLine(i*10,20,i*10,270);//画格子
        }
    QFont font1("Courier",20);
    painter.setFont(font1);
    painter.setPen(Qt::yellow);
    painter.setBrush(Qt::yellow);
    painter.drawText(40,150,display);//显示游戏开始和结束
    QFont font2("Courier",15);
    painter.setFont(font2);
    painter.setPen(Qt::blue);
    painter.setBrush(Qt::blue);
    painter.drawText(140,300,"得分：");
    painter.drawText(230,300,QString::number(score));//显示游戏得分
    painter.setPen(Qt::white);
    painter.setBrush(Qt::green);
    //painter.drawRect(snakerect);//画蛇
    //painter.drawRect(food);
    painter.drawPixmap(food,QPixmap(":/image/1.jpg"));
    painter.drawRects(&snakerect[0],snakerect.size());
    if(over){
        timer->stop();
    }
}
void snake::initalsnake(){
    run=true;
    over=false;
    direction=2;
    display="Game Start";
    score=0;
    food=creatfood();
    //QRect rect(100,70,10,10);
    //snakerect=rect;
    snakerect.resize(5);//蛇的长度
    for(int i=0;i<snakerect.size();i++){
        QRect rect(100,70+10*i,10,10);
        snakerect[snakerect.size()-1-i]=rect;
    }
    timer=new QTimer(this);//设定计时器
    timer->start(speed);
    connect(timer,SIGNAL(timeout()),SLOT(snakeupdate()));
}
 void snake::snakeupdate(){
     display="";
     snakehead=snakerect.first();
     eat();
     hit();
     for(int j=0;j<snakerect.size()-1;j++){
         snakerect[snakerect.size()-1-j]= snakerect[snakerect.size()-2-j];
     }
         switch(direction){
        case 1:
            snakehead.setTop(snakehead.top()-10);
            snakehead.setBottom(snakehead.bottom()-10);
            break;
        case 2:
            snakehead.setTop(snakehead.top()+10);
            snakehead.setBottom(snakehead.bottom()+10);
            break;
        case 3:
            snakehead.setLeft(snakehead.left()-10);
            snakehead.setRight(snakehead.right()-10);
            break;
        case 4:
            snakehead.setLeft(snakehead.left()+10);
            snakehead.setRight(snakehead.right()+10);
            break;
        default:;
        }
        snakerect[0]=snakehead;
        if(snakehead.left()<20||snakehead.right()>270||snakehead.top()<20||snakehead.bottom()>270){
            over=true;
            display="Game Over";
        }
        update();
  }
 void snake:: keyPressEvent(QKeyEvent *event){
      QKeyEvent *key=(QKeyEvent*)event;
      switch(key->key()){
      case Qt::Key_Up:direction=1;
          break;
      case Qt::Key_Down:direction=2;
          break;
      case Qt::Key_Left:direction=3;
          break;
      case Qt::Key_Right:direction=4;
          break;
      default:;
      }
  }
 QRect snake::creatfood(){
     int x,y;
     x=rand()%25;
     y=rand()%25;
     QRect rect(20+x*10,20+y*10,10,10);
     return rect;

 }//在随机位置产生食物
 void snake::eat(){
     if(snakehead==food){
         snakehead==food;
         snakerect.push_back(snakerect.last());
         food=creatfood();
         score=score+10;
         speed=speed-120;
         timer->stop();
         timer->start(speed);
     }
 }//吃到食物
 void snake::hit(){
     for(int i=1;i<snakerect.size();i++){
         if(snakehead==snakerect[i]){
             display="Game Over";
             over=true;
             update();
         }
     }
 }//蛇撞到自己也会使游戏结束
